<template>
  <div class="found-container">
    <el-card>
      <div class="search-bar">
        <el-input 
          v-model="searchParams.keyword" 
          placeholder="请输入关键词搜索" 
          style="width: 300px"
          clearable
        >
          <template #prefix>
            <el-icon><Search /></el-icon>
          </template>
        </el-input>
        <el-select v-model="searchParams.category" placeholder="物品类别" clearable style="width: 150px; margin-left: 10px">
          <el-option v-for="(name, index) in categoryNames" :key="index" :label="name" :value="index" />
        </el-select>
        <el-select v-model="searchParams.locationArea" placeholder="拾获地点" clearable style="width: 150px; margin-left: 10px">
          <el-option v-for="area in areaList" :key="area" :label="area" :value="area" />
        </el-select>
        <el-button type="primary" @click="handleSearch" style="margin-left: 10px">
          <el-icon><Search /></el-icon>
          搜索
        </el-button>
      </div>
    </el-card>

    <div class="item-list" v-loading="loading">
      <el-row :gutter="20">
        <el-col :span="8" v-for="item in itemList" :key="item.id">
          <el-card shadow="hover" class="item-card" @click="viewDetail(item.id)">
            <div class="item-header">
              <div class="item-name">{{ item.itemName }}</div>
              <el-tag size="small" type="success">{{ getCategoryName(item.category) }}</el-tag>
            </div>
            <div class="item-desc">{{ item.description }}</div>
            <div class="item-info">
              <div class="info-item">
                <el-icon><Calendar /></el-icon>
                <span>{{ formatDate(item.lostDate) }}</span>
              </div>
              <div class="info-item">
                <el-icon><Location /></el-icon>
                <span>{{ item.locationArea }}</span>
              </div>
            </div>
          </el-card>
        </el-col>
      </el-row>

      <el-empty v-if="!loading && itemList.length === 0" description="暂无数据" />

      <div class="pagination" v-if="total > 0">
        <el-pagination
          v-model:current-page="searchParams.current"
          v-model:page-size="searchParams.size"
          :total="total"
          layout="total, prev, pager, next"
          @current-change="loadData"
        />
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref, reactive, onMounted } from 'vue'
import { useRouter } from 'vue-router'
import { getItemList } from '@/api/item'

const router = useRouter()
const loading = ref(false)
const itemList = ref([])
const total = ref(0)

const categoryNames = ['电子设备', '证件卡类', '学习用品', '生活用品', '衣物鞋帽', '其他']
const areaList = ['教学楼', '图书馆', '食堂', '宿舍区', '操场', '其他']

const searchParams = reactive({
  type: 1, // 招领
  keyword: '',
  category: null,
  locationArea: '',
  current: 1,
  size: 9
})

const getCategoryName = (category) => categoryNames[category] || '未知'

const formatDate = (date) => {
  return new Date(date).toLocaleDateString()
}

const viewDetail = (id) => {
  router.push(`/detail/${id}`)
}

const handleSearch = () => {
  searchParams.current = 1
  loadData()
}

const loadData = async () => {
  loading.value = true
  try {
    const res = await getItemList(searchParams)
    itemList.value = res.data.records
    total.value = res.data.total
  } catch (error) {
    console.error(error)
  } finally {
    loading.value = false
  }
}

onMounted(() => {
  loadData()
})
</script>

<style scoped>
.found-container {
  max-width: 1200px;
  margin: 0 auto;
}

.search-bar {
  display: flex;
  align-items: center;
}

.item-list {
  margin-top: 20px;
  min-height: 400px;
}

.item-card {
  margin-bottom: 20px;
  cursor: pointer;
  transition: transform 0.3s;
}

.item-card:hover {
  transform: translateY(-5px);
}

.item-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 10px;
}

.item-name {
  font-size: 18px;
  font-weight: bold;
  flex: 1;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.item-desc {
  font-size: 14px;
  color: #606266;
  margin-bottom: 15px;
  height: 40px;
  overflow: hidden;
  text-overflow: ellipsis;
  display: -webkit-box;
  -webkit-line-clamp: 2;
  -webkit-box-orient: vertical;
}

.item-info {
  display: flex;
  justify-content: space-between;
  font-size: 13px;
  color: #909399;
}

.info-item {
  display: flex;
  align-items: center;
}

.info-item .el-icon {
  margin-right: 5px;
}

.pagination {
  display: flex;
  justify-content: center;
  margin-top: 30px;
}
</style>

